package com.fb;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.dev.IconViewClose;
import com.dev.MainActivity;
import com.dev.ShowSoftInput;
import com.dev.TextButton;
import com.fb.FileBrowserPage;
import com.log;
import java.io.File;
import java.util.ArrayList;
public class QueryRenameFile extends LinearLayout implements OnClickListener,TextWatcher,OnEditorActionListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		File dest;
		LinearLayout titlebar;
		LinearLayout bottombar;
		LayoutParams titlebarlp;
		TextView title;
		TextButton it0,it1;
		IconViewClose ivc;
		ShowSoftInput ssi;
		LayoutParams tblp;
		EditText editor;
		FileBrowserPage fbp;
		MainActivity ma;
		public QueryRenameFile(MainActivity ma){
			super(ma);
			this.ma=ma;
			tblp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			setOrientation(VERTICAL);
			setWillNotDraw(false);
			title=new TextView(ma);
			title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			title.setGravity(Gravity.CENTER_VERTICAL);
			title.setTextColor(0xff7f7f7f);
			title.setText("重命名：");
			title.setPadding(9,0,0,0);
			ivc=new IconViewClose(ma);
			ivc.setOnClickListener(this);
			ssi=new ShowSoftInput(ma);
			ssi.setOnClickListener(this);
			titlebar=new LinearLayout(ma){
				@Override
				public void onDraw(Canvas c){
					super.onDraw(c);
					Paint p=new Paint();
					p.setColor(FileBrowserPage.borderColor);
					c.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
				}
			};
			titlebar.setWillNotDraw(false);
			titlebar.setOrientation(HORIZONTAL);
			titlebar.addView(title,new LayoutParams(0,-1,1));
			titlebar.addView(ssi);
			titlebar.addView(ivc);
			titlebarlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			it0=new TextButton(ma,"关闭");
			it0.setOnClickListener(this);
			it0.borderColor=FileBrowserPage.borderColor;
			it0.borderWidth=FileBrowserPage.borderWidth;
			it0.borderTop=true;
			it1=new TextButton(ma,"确认");
			it1.setOnClickListener(this);
			it1.borderColor=FileBrowserPage.borderColor;
			it1.borderWidth=FileBrowserPage.borderWidth;
			it1.borderTop=true;
			editor=new EditText(ma){
				public void onDraw(Canvas cvs){
					super.onDraw(cvs);
					Paint p=new Paint();
					p.setStrokeWidth(3);
					if(isFocused()){
						p.setColor(0xffff0000);
					}else{
						p.setColor(0xff777777);
					}
					int ver=getHeight()/2+(int)(getTextSize()*0.7);
					cvs.translate(getScrollX(),getScrollY());
					cvs.drawLine(getPaddingLeft(),ver,getRight()-getPaddingRight(),ver,p);
				}
			};
			editor.addTextChangedListener(this);
			editor.setTypeface(Typeface.MONOSPACE);
			editor.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
			editor.setTextColor(0xff000000);
			editor.setGravity(Gravity.CENTER_VERTICAL);
			editor.setBackgroundColor(0x00ffffff);
			editor.setPadding(10,0,10,0);
			editor.setSingleLine();
			editor.setOnEditorActionListener(this);
			bottombar=new LinearLayout(ma);
			bottombar.setOrientation(HORIZONTAL);
			LinearLayout.LayoutParams itlp=new LinearLayout.LayoutParams(0,-1,1);
			bottombar.addView(it0,itlp);
			bottombar.addView(it1,itlp);
			addView(titlebar,titlebarlp);
			addView(editor,-1,(int)(33*ma.dp));
			addView(bottombar,-1,(int)(30*ma.dp));
		}
		public void show(FileBrowserPage fbp,File dest){
			this.dest=dest;
			this.fbp=fbp;
			wlp.width=ma.gui.getWidth()*4/5;
			wlp.height=-2;
			String name=dest.getName();
			int dot=name.lastIndexOf('.');
			editor.setText(name);
			if(dot>0){
				editor.setSelection(0,dot);
			}else if(dot<=0){
				editor.setSelection(0,name.length());
			}
			ma.wm.addView(this,wlp);
		}
		void dismiss(){
			ma.wm.removeView(this);
		}
		@Override
		public boolean onEditorAction(TextView v,int actionId,KeyEvent event){
			String name=editor.getText().toString();
			File f=new File(dest.getParent(),name);
			if(f.exists()){
				if(f.equals(dest)){
					Toast.makeText(ma,"文件名未改变",0).show();
					return true;
				}else{
					Toast.makeText(ma,"文件已存在",0).show();
					return true;
				}
			}
			return false;
		}
		@Override
		public void onClick(View v){
			if(v==ivc){//onClick
				dismiss();
			}else if(v==ssi){//onClick
				if(editor.requestFocus()){
					ma.imm.showSoftInput(editor,0);
                    editor.selectAll();
				}
			}else if(v==it1){//onClick,confirmed rename file
				String nameWill=editor.getText().toString();
				if(nameWill.contains("/")){
					Toast.makeText(ma,"无效的文件名",0).show();
					return;
				}
				File userInput=new File(dest.getParent(),nameWill);
				if(userInput.exists()){
					if(userInput.equals(dest)){
						Toast.makeText(ma,"文件名未改变",0).show();
					}else{
						Toast.makeText(ma,"文件已存在",0).show();
					}
				}else {
					try{
						if(dest.renameTo(userInput)){
							dismiss();
							Toast.makeText(ma,"重命名成功",0).show();
							if(fbp.dyn==10||fbp.dyn==11){
								ArrayList<File> fs=new ArrayList<File>();
								try{
									ContentResolver mContentResolver=ma.getContentResolver();
									Cursor c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
									int column = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
									while(c.moveToNext()){
										String path = c.getString(column);
										File tmp=new File(path);
										if(dest.equals(tmp)){//file was renamed
											//MediaStore.createWriteRequest(
											Toast.makeText(ma,"更新媒体存储",0).show();
											ContentValues cv=new ContentValues();
											cv.put(MediaStore.MediaColumns.DATA,userInput.getAbsolutePath());
											//cv.put(MediaStore.Audio.
											//String path_=path.replace("
											if(mContentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.MediaColumns.DATA+"=?",new String[]{path})<=0){
												log.alert("更新媒体存储失败");
											}
											Intent it=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://"+userInput.getAbsolutePath()));
											ma.sendBroadcast(it);
											//PreparedStatement ps=new PreparedStatement("");
											fs.add(userInput);
										}else{
											fs.add(tmp);
										}
									}
								}catch(Exception e){
									log.alert(e.toString());
								}
								File[] files=new File[fs.size()];
								fs.toArray(files);
								fbp.files=files;
								fbp.setWillShow(files);
							}else{
								fbp.setWillShow(fbp.currentDirectory.listFiles());
							}
						}else{
							Toast.makeText(ma,"重命名失败",0).show();
						}
					}catch(Exception e){
						log.alert("在重命名文件时",e.toString());
					}
				}
			}else if(v==it0){//onClick
				dismiss();
			}
		}
		@Override
		protected void onDraw(Canvas canvas){
			super.onDraw(canvas);
			Paint p=new Paint();
			p.setColor(FileBrowserPage.borderColor);
			canvas.drawColor(0xffffffff);
			canvas.drawRect(0,0,FileBrowserPage.borderWidth,getHeight(),p);
			canvas.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
			canvas.drawRect(getWidth()-FileBrowserPage.borderWidth,0,getWidth(),getHeight(),p);
			canvas.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
		}
		@Override
		public void onTextChanged(CharSequence s,int start,int before,int count){
		}
		@Override
		public void afterTextChanged(Editable s){
		}
		@Override
		public void beforeTextChanged(CharSequence s,int start,int count,int after){
		}
	}
